package com.springsecurity.demo.controller;


import com.springsecurity.demo.common.Response;
import com.springsecurity.demo.model.dto.AuthLoginRequest;
import com.springsecurity.demo.model.entity.SysUser;
import com.springsecurity.demo.model.entity.Users;
import com.springsecurity.demo.service.ISysUserService;
import com.springsecurity.demo.service.LoginServcie;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.util.MapUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(description = "spring security demo login", tags = {"spring security demo login"})
@Controller
@RequestMapping("/spring_security_demo/login")
public class LoginController {

    private static final Logger log = LoggerFactory.getLogger(LoginController.class);
    @Autowired
    private LoginServcie loginServcie;

    @Resource
    private ISysUserService iSysUserService;

//    @PostMapping("/user/login")
    @ApiOperation(value = "login", notes = "login")
    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    @ResponseBody
    public Response login(@RequestBody AuthLoginRequest user){
        //登录
        Map<String, String> login = loginServcie.login(user);
        if (login != null && login.size()> 0) {
            return Response.success(login);
        }
        return Response.failure("login failed");
    }

    @ApiOperation(value = "login", notes = "login")
    @RequestMapping(value = "/user/logout", method = RequestMethod.POST)
    @ResponseBody
    public Response logout(@RequestHeader(name = "token", required = false) String token){
        if (token == null) {
            return Response.failure("logout failed cause token is null");
        }
        boolean logout = loginServcie.logout(token);
        return Response.success(logout);

    }
}
